<!--
 * @Description: 下载附件
-->
<template>
    <div class="download-att">
        <!-- 关联文档 -->
        <template v-if="isDoc">
            <div v-if="file.mimeType === 'uc'" @click="openZxchatMessage(file.description)">
                <slot></slot>
            </div>
            <a v-else :href="createDocUrl(file)" target="_blank"><slot></slot></a>
        </template>
        <!-- 表单中的附件 -->
        <a v-else-if="isFormFile" @click="downloadAtt" target="downloadFileFrame"><slot></slot> </a>
        <!-- 下载 -->
        <a
            v-else
            :href="`${ctxPath}/fileUpload.do?method=doDownload4html&fileId=${file.fileUrl}&v=${
                file.v
            }&createDate=${
                file.createdate && file.createdate.toString().substring(0, 10)
            }&filename=${encodeURIComponent(file.filename)}`"
            download
        >
            <slot></slot>
        </a>
    </div>
</template>
<script>
    import globalData from '@/plugins/data';

    export default {
        name: 'DownloadAtt',
        props: {
            file: {
                type: Object,
            },
        },
        created() {
            this.ctxPath = globalData.ctxPath;
        },
        computed: {
            //是否是关联文档
            isDoc() {
                return (this.file.type.toString() === '2' || this.file.type.toString() === '4') && this.file.description;
            },
            isFormFile() {
                return this.file.type.toString() === '3';
            },
        },
        methods: {
            /**
             * @description: 生成表关联文档用的跳转链接
             * @return {String}返回链接
             */
            createDocUrl() {
                let suffix = ''; //表单子流程中的关联文档 关联的是主流程id，需要在此传入子流程id，用于判断权限.
                // if (this.type == 4) {
                //     try {
                //         if (
                //             parent.parent.openerSummaryId &&
                //             parent.parent.openerSummaryId != this.reference
                //         ) {
                //             suffix = '&openerSummaryId=' + parent.parent.openerSummaryId;
                //         } else {
                //             if (noFlowRecordId) {
                //                 suffix = '&noFlowRecordId=' + noFlowRecordId;
                //             }
                //         }
                //     } catch (e) {}
                // }
                const { mimeType, description, reference, category } = this.file;
                switch (mimeType) {
                    case 'collaboration':
                        return `${_ctxPath}/collaboration/collaboration.do?method=summary&openFrom=glwd&type=&affairId=${description}&baseObjectId=${reference}&baseApp=${category}${CsrfGuard.getUrlSurffix()}`;
                    case 'edoc':
                        return `${_ctxPath}/edocController.do?method=detailIFrame&isQuote=true&from=Done&openFrom=glwd&affairId=${description}&baseObjectId=${reference}&baseApp=${category}${CsrfGuard.getUrlSurffix()}`;
                    case 'km':
                        return `${_ctxPath}/doc.do?method=docOpenIframeOnlyId&openFrom=glwd&docResId=${description}&baseObjectId=${reference}&baseApp=${category}${suffix}${CsrfGuard.getUrlSurffix()}`;
                    case 'meeting':
                        return `${_ctxPath}/meeting.do?method=view&isQuote=true&state=10&meetingId=${description}&baseObjectId=${reference}&baseApp=${category}${CsrfGuard.getUrlSurffix()}`;
                    case 'info':
                        return `${_ctxPath}/info/infoDetail.do?method=summary&refAffairId=${description}&baseObjectId=${reference}${CsrfGuard.getUrlSurffix()}`;
                }
            },
            /**
             * @description:以前的代码，贴过来。看描述是通过id获取致信的什么title
             * @param {string}chatMessageId 致信消息ID
             * @return {*}
             */
            openZxchatMessage(chatMessageId) {
                url = `${_ctxPath}/zx/chatmessage.do?method=showChatMessage&chatMessageId=${chatMessageId}`;
                callBackendMethod('zxChatMessageManager', 'getTitle', chatMessageId, {
                    success: function (returnVal) {
                        var dialog = $.dialog({
                            url: url,
                            width: 656,
                            height: 514,
                            isDrag: false,
                            title: returnVal,
                            targetWindow: getCtpTop(),
                            buttons: [
                                {
                                    id: 'btncancel',
                                    text: $.i18n('common.button.close.label'),
                                    handler: function () {
                                        dialog.close();
                                    },
                                },
                            ],
                        });
                    },
                    error: function (returnVal) {
                        if (typeof console !== 'undefined' && typeof console.log !== 'undefined') {
                            console.log(returnVal);
                        }
                    },
                });
            },
            /**
             * @description 下载附件，原逻辑在v3x-debug.js
             */
            downloadAtt() {
                const { fileUrl, filename, createdate } = this.file;
                downloadAttachment(
                    fileUrl,
                    createdate.toString().substring(0, 10),
                    encodeURIComponent(filename),
                );
            },
        },
    };
</script>
<style lang="scss" scoped></style>
